package com.liangzhong.modules.explore.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ModulePanelTypeConst;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.ExploreEvent;
	import com.liangzhong.events.GuideEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxyconstant.explore.ExploreProxyConstant;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.explore.IExploreView;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	
	public class ExploreMediator extends ModuleMediator
	{
		public static const MediatorName :String = "ExploreMediator";
		public function ExploreMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get view():IExploreView
		{
			return viewComponent as IExploreView;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onremoveModule);
			(view as DisplayObject).addEventListener(ExploreEvent.UP_EXPLORE,onUpHandler);
			(view as DisplayObject).addEventListener(ExploreEvent.BUY_EXPLORE_TIME,onBuyHandler);
			(view as DisplayObject).addEventListener(ExploreEvent.add_feel,onAddFeelHandler);
			(view as DisplayObject).addEventListener(ExploreEvent.SIGN,onSignHandler);
			(view as DisplayObject).addEventListener(ModuleEvent.DROP_TIME,onClearUPTime);
			AppEventDispatcher.getInstance().addEventListener(ExploreEvent.EXPLORE_PLAYER,onExplorePlayerHandler);
		}
		
		protected function onremoveModule(event:Event):void
		{
			// TODO Auto-generated method stub
			removeMediator();
		}
		
		/**
		 *探索球员 
		 * @param event
		 * 
		 */
		protected function onExplorePlayerHandler(event:ExploreEvent):void
		{
			this.sendNotification(ExploreProxyConstant.EXPLORE_MEDIATORTOCOMMAND,event.param,ExploreProxyConstant.EXPLORE_PLAYER);
		}
		
		/**
		 *清除冷却时间 
		 * @param event
		 * 
		 */
		protected function onClearUPTime(event:ExploreEvent):void
		{
			this.sendNotification(ExploreProxyConstant.EXPLORE_MEDIATORTOCOMMAND,event.param,ExploreProxyConstant.CLEAR_TIME);
		}
		
		/**
		 *签约 
		 * @param event
		 * 
		 */
		protected function onSignHandler(event:ExploreEvent):void
		{
			this.sendNotification(ExploreProxyConstant.EXPLORE_MEDIATORTOCOMMAND,event.param,ExploreProxyConstant.SIGN);
		}
		
		/**
		 *增加好感度 
		 * @param event
		 * 
		 */
		protected function onAddFeelHandler(event:ExploreEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(ExploreProxyConstant.EXPLORE_MEDIATORTOCOMMAND,event.param,ExploreProxyConstant.ADD_FEEL);
		}
		
		/**
		 *购买次数 
		 * @param event
		 * 
		 */
		protected function onBuyHandler(event:ExploreEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(ExploreProxyConstant.EXPLORE_MEDIATORTOCOMMAND,event.param,ExploreProxyConstant.BUY_EXPLORE_TIME);
		}
		
		/**
		 *升级探索 
		 * @param event
		 * 
		 */
		protected function onUpHandler(event:ExploreEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(ExploreProxyConstant.EXPLORE_MEDIATORTOCOMMAND,event.param,ExploreProxyConstant.UP_EXPLORE);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onremoveModule);
			(view as DisplayObject).removeEventListener(ExploreEvent.UP_EXPLORE,onUpHandler);
			(view as DisplayObject).removeEventListener(ExploreEvent.BUY_EXPLORE_TIME,onBuyHandler);
			(view as DisplayObject).removeEventListener(ExploreEvent.add_feel,onAddFeelHandler);
			(view as DisplayObject).removeEventListener(ExploreEvent.SIGN,onSignHandler);
			(view as DisplayObject).removeEventListener(ModuleEvent.DROP_TIME,onClearUPTime);
			AppEventDispatcher.getInstance().removeEventListener(ExploreEvent.EXPLORE_PLAYER,onExplorePlayerHandler);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [NotificationConstants.COMMON_DROP_TIME,
				ExploreProxyConstant.GET_UP_EXPLORE,
				ExploreProxyConstant.GET_BUY_EXPLORE_TIME,
				ExploreProxyConstant.GET_ADD_FEEL,
				ExploreProxyConstant.GET_SIGN,
				ExploreProxyConstant.GET_EXPLORE_INFO,
				ExploreProxyConstant.GET_EXPLORE_PLAYER].concat(super.listNotificationInterests());
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case NotificationConstants.COMMON_DROP_TIME://冷却时间返回
					if(notification.getBody() == BuildingType.EXPLORE)
					{
						AppData.getInstance().exploreDataManager.exploreInfo.pretime = 0;
//						view.clearTime();
					}
					break;
				case ExploreProxyConstant.GET_UP_EXPLORE:
//					view.upExplore(notification.getBody());
					break;
				case ExploreProxyConstant.GET_BUY_EXPLORE_TIME:
					view.buyExploreTime(notification.getBody());
					break;
				case ExploreProxyConstant.GET_ADD_FEEL:
					view.addFeel(notification.getBody());
					break;
				case ExploreProxyConstant.GET_SIGN:
					view.sign(notification.getBody());
					break;
				case ExploreProxyConstant.GET_EXPLORE_INFO:
					if(view == null && ResourceManager.instance.isContainResource(ResourceConst.EXPLORE_SWF))
						showUI();
					break;
				case ExploreProxyConstant.GET_EXPLORE_PLAYER:
					view.explorePlayer(notification.getBody());
					break;
				
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			if(AppData.getInstance().exploreDataManager.exploreInfo == null)
			{
				this.sendNotification(ExploreProxyConstant.EXPLORE_MEDIATORTOCOMMAND,null,ExploreProxyConstant.EXPLORE_INFO);
			}
			resName = ResourceConst.EXPLORE_SWF;
			if(ResourceManager.instance.isContainResource(ResourceConst.EXPLORE_SWF))
			{
				showUI();
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).explore[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			showUI();
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.EXPLORE_SWF,resList)
		}
		private function showUI():void
		{
			if(AppData.getInstance().exploreDataManager.exploreInfo == null)
			{
				return;
			}
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.explore.ExploreView");
			
			viewComponent = cView;
			//this.sendNotification(NotificationConstants.MAINVIEW_ADD_DISPLAYOBJECT,{type:ModulePanelTypeConst.EXPLOR,dispalyObj:view.bg});
			cView.initUI();
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			addEventListeners();
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
			this.sendNotification(NotificationConstants.MAINVIEW_REMOVE_DISPLAYOBJECT);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}